package com.example.last.service;

import cn.hutool.http.HtmlUtil;
import com.example.last.dao.PropagateDao;
import com.example.last.entity.Params;
import com.example.last.entity.Propagate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PropagateService {
    @Resource
    private PropagateDao propagateDao;

    public PageInfo<Propagate> findBySearch(@Param("params") Params params){
        //开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<Propagate> list =  propagateDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Propagate propagate){
        propagateDao.add(propagate);
    }

    public void update(Propagate propagate){
        propagateDao.update(propagate);
    }

    public void delete(Integer id){propagateDao.delete(id);}


    public List<Propagate> findAll() {
        List<Propagate> list =  propagateDao.findAll();
        for(Propagate dbpropagate : list) {
            dbpropagate.setDescription(HtmlUtil.cleanHtmlTag(dbpropagate.getContent()));
        }
        return list;
    }

    public Propagate findById(Integer id) {
        return propagateDao.findById(id);
    }

    public List<Propagate> findAllData() {
        List<Propagate> list =  propagateDao.findAllData();
        for(Propagate dbpropagate : list) {
            dbpropagate.setDescription(HtmlUtil.cleanHtmlTag(dbpropagate.getContent()));
        }
        return list;
    }
}
